package com.xiaoxing.zhinenghuacangku.mvp.presenter;

import android.app.Application;
import android.content.Context;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.jess.arms.utils.RxLifecycleUtils;
import com.xiaoxing.zhinenghuacangku.mvp.contract.RuKuQingZhangContract;
import com.xiaoxing.zhinenghuacangku.mvp.model.entity.GetUserSpaceListForAutoStore;
import com.xiaoxing.zhinenghuacangku.mvp.ui.entity.RuKuQingZhang;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class RuKuQingZhangPresenter extends BasePresenter<RuKuQingZhangContract.Model, RuKuQingZhangContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public RuKuQingZhangPresenter(RuKuQingZhangContract.Model model, RuKuQingZhangContract.View rootView) {
        super(model, rootView);
    }

    public void getRuKuQingZhangData(Map<String, String> map) {

        mModel.getRuKuQingZhangData(map).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<RuKuQingZhang>(mErrorHandler) {
                    @Override
                    public void onNext(RuKuQingZhang entityData) {
                        mRootView.getRuKuQingZhangDataSuccess(entityData);
                    }
                });
    }

    public void callAgvGetMaterialToInstoreBuffer(Context context, String mid) {

        mModel.callAgvGetMaterialToInstoreBuffer(mid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String callAgvGetMaterialToInstoreBuffer) {
                        String result = null;
                        LogUtils.debugEInfo("result===" + result);
                        try {
                            result = callAgvGetMaterialToInstoreBuffer.substring(75, callAgvGetMaterialToInstoreBuffer.toCharArray().length - 9);
                            if (result.equals("[]")) {
                                ArmsUtils.makeText(context, "");
                            } else {
                                mRootView.callAgvGetMaterialToInstoreBufferSuccess(result);
                            }
                        } catch (Exception e) {
                            ArmsUtils.makeText(context, "无效的二维码");
                        }

                    }
                });
    }

    public void checkCanReInstore(Context context, String mid) {

        mModel.checkCanReInstore(mid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String checkCanReInstore) {
                        String result = null;
                        try {
                            result = checkCanReInstore.substring(76, checkCanReInstore.toCharArray().length - 10);
                            if (result.equals("[]")) {
                                ArmsUtils.makeText(context, "");
                            } else {
                                mRootView.checkCanReInstoreSuccess(result);
                            }
                        } catch (Exception e) {
                        }

                    }
                });
    }

    public void instoreAgain(Context context, String mid) {

        mModel.instoreAgain(mid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String instoreAgain) {
                        String result = null;
                        try {
                            result = instoreAgain.substring(72, instoreAgain.toCharArray().length - 6);
                            if (result.equals("[]")) {
                                ArmsUtils.makeText(context, "");
                            } else {
                                mRootView.instoreAgainSuccess(result);
                            }
                        } catch (Exception e) {
                        }

                    }
                });
    }

    public void getUserSpaceListForAutoStore(Context context, String user_id, String spname) {

        mModel.getUserSpaceListForAutoStore(user_id, spname).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String getUserSpaceListForAutoStore) {
                        String result = getUserSpaceListForAutoStore.substring(75, getUserSpaceListForAutoStore.toCharArray().length - 9);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "没有搜索到相关数据");
                        } else {
                            List<GetUserSpaceListForAutoStore> getUserSpaceListForAutoStoreList = ArmsUtils.parseString2List(result, GetUserSpaceListForAutoStore.class);
                            mRootView.getUserSpaceListForAutoStoreSuccess(getUserSpaceListForAutoStoreList);
                        }

                    }
                });
    }

    public void addAgvTask(Context context, String mid, String instoreBufferId, String spid, String num, String uid, String remark) {

        mModel.addAgvTask(mid, instoreBufferId, spid, num, uid, remark).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String createMaterialForInstoreAuto) {
                        String result = createMaterialForInstoreAuto.substring(72, createMaterialForInstoreAuto.toCharArray().length - 6);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<GetUserSpaceListForAutoStore> getUserSpaceListForAutoStoreList = ArmsUtils.parseString2List(result, GetUserSpaceListForAutoStore.class);
                            mRootView.addAgvTaskSuccess(result);
                        }
                    }
                });
    }

    public void setInstoreBufferReadyForAGV(Context context, String bufferId, String spid) {


        mModel.setInstoreBufferReadyForAGV(bufferId, spid).subscribeOn(Schedulers.io())
                //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                .doOnSubscribe(disposable -> {
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    mRootView.hideLoading();
                })
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<String>(mErrorHandler) {
                    @Override
                    public void onNext(String createMaterialForInstoreAuto) {
                        String result = createMaterialForInstoreAuto.substring(76, createMaterialForInstoreAuto.toCharArray().length - 10);
                        if (result.equals("[]")) {
                            ArmsUtils.makeText(context, "");
                        } else {
//                            List<GetUserSpaceListForAutoStore> getUserSpaceListForAutoStoreList = ArmsUtils.parseString2List(result, GetUserSpaceListForAutoStore.class);
                            mRootView.setInstoreBufferReadyForAGVSuccess(result);
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
